<?php
class InviteController extends Controller{


	public function index(){
		$this->getSmarty();
		
		$logined = false;
		if( isset($_SESSION['user'])) {
			$logined = true;
		}
		
		//加入用户注册邀请链接
		$this->smarty->assign ( 'inviteLink', self::getULink());
		
		//加入用户最近邀请人信息列表
		$this->smarty->assign ( 'userInviteList', self::listInviteList(0));
		//加入公共邀请列表信息
		$this->smarty->assign ( 'comInviteList', self::listInviteList(1));
		
		$this->smarty->assign ( 'logined', $logined);
		
		$this->smarty->display("user_center/invite.tpl");
	}

	
	public function getULink(){
		require_once COMMON . DS . 'Constant.class.php';
		$s_user = self::getSessonUser();
		return WEBSITE_URL.signup.'?i='.$s_user['userid'];
	}
	public function inviteEmails(){
		$fmail = $_POST ['f_email'];
		$smail = $_POST ['s_email'];
		$msg = $_POST ['msg'];
		$mresult="";
		if(!empty($fmail)){
			$mresult = $mresult.self::sendMailToUser($fmail,$msg)."    ";
		}
		if(!empty($smail)){
			$mresult = $mresult.self::sendMailToUser($smail,$msg);
		}
		echo json_encode(strip_tags($mresult)); 
	}
	
	public function listInviteList($type){
	
	 	require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );
		$results = array();
		//获取用户自己的
		if($type ==0){
			$s_user = self::getSessonUser();
			$sql="SELECT DATE_FORMAT(create_time,'%m.%d') create_time,invite_user_mail,state,rebate FROM `user_invite_record` where userid='".$s_user['userid']."'  ORDER BY create_time limit 10";
			$results = $service->getResults($sql);
		}
		if($type ==1){
			$sql="SELECT username, DATE_FORMAT(create_time,'%m.%d') create_time,invite_user_mail,state,rebate  FROM `user_invite_record`   ORDER BY create_time limit 10";
			$results = $service->getResults($sql);
		}
		return $results;
	}
	
	public function insertInviteList($mail){
	
		$s_user = self::getSessonUser();
	 	require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );
		
		
		//判断当前邮件是否被邀请过
		$query = "userid = '". $s_user['userid']."' and invite_user_mail= '".$mail."'";

		$count = $service->countNum('user_invite_record',$query);
		if($count != 0){
			return;
		}
		$date = date("Y-m-d h:m:s");
		$uid = uniqid ();
		$record = array (
				'id' => $uid,
				'userid' => $s_user['userid'],
				'username' => $s_user['username'],
				'invite_user_mail' => $mail,
				'state' => 0,
				'rebate' => 0,
				'create_time' => $date
		);
		$service->add('user_invite_record',$record);
	}
	public function sendMailToUser($mail,$msg){
		$s_user = self::getSessonUser();
		require_once COMMON . DS . 'SendMailUtil.class.php';
		$result = SendMailUtil::sendmail ( $s_user->username."invite you to babyspeedy", $msg.' Click  '.self::getULink(), $mail );
		if($result == "Success"){
			self::insertInviteList($mail);
		 	return $mail." send  success";
		}else{
			return $result;
		}
	}
	
	
	/**
	 * 获取session中的user对象..返回结果是数组形式
	 * Enter description here ...
	 */
	function getSessonUser() {
		if(isset($_SESSION['user'])){
			$s_user = $_SESSION['user'];
			return self::object2array($s_user);
		}else{
			return null;
		}
	}
/**
 *object->array
 *
 **/
function object2array(&$cgi,$type=0) {
    if(is_object($cgi)) {
        $cgi = get_object_vars($cgi);
    }
    if(!is_array($cgi)) {
        $cgi = array();
    }
    foreach($cgi as $kk=>$vv) {
        if(is_object($vv)) {
            $cgi[$kk] = get_object_vars($vv);
            self::object2array($cgi[$kk],$type);
        }
        else if(is_array($vv)) {
            self::object2array($cgi[$kk],$type);
        } else {
            $v = $vv;
            $k = $kk;
            $cgi["$k"] = $v;
        }
    }
    return $cgi;
}
}